package tx.core.graphics;

import java.awt.Image;

/**
 * An animation that moves.
 * This class is based on Sprite class by David Brackeen.
 * 
 * @author Guilherme Mauro Germoglio Barbosa - germoglio@gmail.com
 */
public class Sprite {

	/**
	 * The animation.
	 */
	private Animation animation;
	
	/**
	 * The x position (in pixels). 
	 */
	private float x;
	
	/**
	 * The y position (in pixels).
	 */
	private float y;
	
	/**
	 * The horizontal velocity (in pixels per millisecond).
	 */
	private float dx;
	
	/**
	 * The vertical velocity (in pixels per millisecond).
	 */
	private float dy;
	
	/**
	 * Creates a new Sprite with the specified Animation.
	 * @param animation The animation.
	 */
	public Sprite(Animation animation) {
		this.animation = animation;
	}
	
	/**
	 * Updates this Sprite's Animation and its position based on the
	 * velocity and the elapsed time.
	 * @param elapsedTime The elapsed time.
	 */
	public void update(long elapsedTime) {
		x += dx * elapsedTime;
		y += dy * elapsedTime;
		animation.update(elapsedTime);
	}
	
	/**
	 * Gets this Sprite's current x position.
	 * @return this Sprite's current x position.
	 */
	public float getX() {
		return x;
	}
	
	/**
	 * Gets this Sprite's current y position.
	 * @return this Sprite's current y position.
	 */
	public float getY() {
		return y;
	}
	
	/**
	 * Sets this Sprite's current x position.
	 * @param x The new x position.
	 */
	public void setX(float x) {
		this.x = x;
	}
	
	/**
	 * Sets this Sprite's current y position.
	 * @param y The new y position.
	 */
	public void setY(float y) {
		this.y = y;
	}
	
	/**
	 * Gets this Sprite's width, based on the size of the current image.
	 * @return this Sprite's width, based on the size of the current image.
	 */
	public int getWidth() {
		return animation.getImage().getWidth(null);
	}
	
	/**
	 * Gets this Sprite's height, based on the size of the current image.
	 * @return this Sprite's height, based on the size of the current image.
	 */
	public int getHeight() {
		return animation.getImage().getHeight(null);
	}
	
	/**
	 * Gets the horizontal velocity of this Sprite.
	 * @return the horizontal velocity of this Sprite in pixels per millisecond.
	 */
	public float getVelocityX() {
		return dx;
	}
	
	/**
	 * Gets the vertical velocity of this Sprite.
	 * @return the vertical velocity of this Sprite in pixels per millisecond.
	 */
	public float getVelocityY() {
		return dy;
	}
	
	/**
	 * Sets the horizontal velocity of this Sprite.
	 * @param dx The new horizontal velocity in pixels per millisecond. 
	 */
	public void setVelocityX(float dx) {
		this.dx = dx;
	}
	
	/**
	 * Sets the vertical velocity of this Sprite.
	 * @param dy The new vertical velocity in pixels per millisecond. 
	 */
	public void setVelocityY(float dy) {
		this.dy = dy;
	}
	
	/**
	 * Returns the Sprite's current image.
	 * @return the Sprite's current image.
	 */
	public Image getImage() {
		return animation.getImage();
	}
}
